/***************************************************************************
 *   Copyright (C) 2005 by ywchen2000                         *
 *   ywchen2000@yahoo.com.cn                                                          *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "readfile.h"
using namespace std;
file_op::readfile::readfile(void)
{
     is_read_finish=false;
     charpos=0;
}
bool file_op::readfile::fileopen(const char* fname)
{
         filename=const_cast<char*>(fname);
      fd=open(filename,O_RDWR|O_CREAT|O_APPEND,S_IRUSR|S_IWUSR);
      nbytes=0;
      is_read_finish=false;
      if(fd<0)
        {
            printf("open file failed");
	    return false;
           }
      return true;
}
file_op::readfile::~readfile(void)
{
	Close();
}
bool file_op::readfile::readbuffer(void)
{
          nbytes=read(fd,buffer,buffer_size);
          if(nbytes>0)
          {
                  charpos=-1;
                  is_read_finish=false;
                  return true;
            }
          else
           {                         //read eof and read over;
                 return false;
               }
}
char* file_op::readfile::getnewline(void)
{
        register char ch;
        register int i=0;
        ch=this->getchar();
        while(ch=='\n' || ch=='\r')      //delete '\n' and '\r'
           {
               ch=this->getchar();
             }
        while(ch!='\n' && i<line_size && ch!=end)
	    {
                  if(ch=='\r' || ch==' ')
                      {
                    ch=this->getchar();                                                                                }
                  else{
                        line[i]=ch;
                        ch=this->getchar();
                        i++;
                      }
              }
           if(ch==end)
                  {
                      line[i]='\0';
                      is_read_finish=true;
                      return line;
                     }
                   line[i]='\0';
                   return line;
}
char file_op::readfile::getchar(void)
{
             charpos++;
            if(nbytes==buffer_size)
                 {
                     if(charpos==buffer_size)
                           {
                             this->readbuffer();
                             charpos++;
                               }
                   }
            else if(nbytes<buffer_size)
                  {
                      if(charpos==nbytes)
                         {
                              //if(this->readbuffer()==false)
                            buffer[charpos]=end;
                          }
                   }
             return buffer[charpos];
}
bool file_op::readfile::read_finish(void)
{
            return is_read_finish;
}
void file_op::readfile::Close(void)
{
            if(close(fd)<0)
                  {
                     printf("close file failed");			
                       }
}
char** file_op::readfile::split(char *string,char ch,int* count)
{
    char *p=string;
    char **value;
    long i=0,len=0,duancount=0,strlens;
    char c;
        strlens=strlen(string)+1;
      if(*p=='\0')
          {
              return NULL;
              }
       while((c=*p++)!='\0')
          {
              if(c==ch && *p!='\0')
                  {
                     if(duancount==0)
                        duancount=2;
                     else
                        duancount++;
                    }
               else if(c==ch && *p=='\0')
                       {
                         if(duancount==0) duancount++;
                        }
          }
           if(duancount==0) { *count=duancount; return NULL;}
           else {  *count=duancount;}
     value=(char **)malloc(duancount*sizeof(char*));           /*0 0 0 0 0 0 0 0 0*/
   value[i]=(char *)malloc(strlens*sizeof(char));              /*0 0 0 0 0 0 0 0 0*/ 
   p=string;
   while((c=*p++)!='\0')
      {
         if(c==ch && *p=='\0')
               {
            *(value[i]+len)='\0';
                break;
                 }
         if(c!=ch && *p=='\0')
              {
                  *(value[i]+len)=c;
                  len++;
                  *(value[i]+len)='\0';
                  break;
                   }
         if(c==ch)
           {
            *(value[i]+len)='\0';
            i++;
   	    value[i]=(char *)malloc(strlens*sizeof(char));
            len=0;
            continue;
            }

         *(value[i]+len)=c;
          len++;
        }
    p=NULL;
    return value;
}
void file_op::readfile::save_str(const char* str)
{
	write(fd,str,strlen(str));	
}
